package ACM;

import java.util. *;

/**
 * 计算最大矩阵值 ---错的
 */
public class Main2 {

    static Scanner scanner = new Scanner(System.in);
    //存储最后的结果集
    static List<Integer> listResult = new ArrayList<>();
    //存储每一行的结果
    static List<String> list = new ArrayList<>();

    public static void main(String[] args) {
        //输入测试样例的组数
        int num = scanner.nextInt();
        for (int i = 0; i < num; i++) {
            //输入第一个数组
            InputArray();
        }

        for (Integer integer : listResult) {
            System.out.println(integer);
        }
    }

    private static void InputArray() {
        int a = scanner.nextInt();
        for (int i = 0; i < a; i++) {
            StringBuilder stringBuilder = new StringBuilder();
            for (int j = 0; j < a; j++) {
                stringBuilder.append(scanner.nextInt());
            }
            list.add(stringBuilder.toString());
        }

        //得到第一行
        String s = list.get(0);
        System.out.println("第一行-->" + s);
        //得到第一个字符
        char first = s.charAt(0);
        if (first == '0') {
            s.replace('0', '1');
        }

        //转为二进制
        TransForm2(s);
    }

    //转为二进制
    private static void TransForm2(String s) {
        int i = Integer.parseInt(s, 2);
        listResult.add(i);
    }

    //计算矩阵的最大值：
    public static int calculateMax(int[][] nums) {
        int max = 0;
        return max;
    }
}

/**
 * 计算矩阵的最大值 ---->  样例是对的
 */
class Main3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int[] arr = new int[a];
        for (int i = 0; i < a; i++) {
            int n = in.nextInt();
            int sum = 0;
            int[][] matrix = new int[n][n];
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < n; k++) {
                    matrix[j][k] = in.nextInt();
                }
            }
            arr[i] = getMax(matrix);
        }
        for (int i : arr) {
            System.out.println(i);
        }
    }

    private static int getMax(int[][] matrix) {
        boolean flag1 = false;
        boolean flag2 = false;
        int max = 1;
        for (int i = 0; i < matrix.length; i++) {
            max = max << 1;
        }
        int n = matrix.length;
        max = max - 1;
        while (!flag1 || !flag2) {
            flag1 = true;
            flag2 = true;
            for (int[] ints : matrix) {
                StringBuilder sb = new StringBuilder();
                for (int i : ints) {
                    sb.append(i);
                }
                int i = Integer.parseInt(sb.toString(), 2);
                if (i <= (max / 2)) {
                    flag1 = false;
                    for (int k = 0; k < ints.length; k++) {
                        ints[k] = ints[k] == 0 ? 1 : 0;
                    }
                }
            }
            for (int i = 0; i < n; i++) {
                StringBuilder ans = new StringBuilder();
                for (int[] ints : matrix) {
                    ans.append(ints[i]);
                }
                int ik = Integer.parseInt(ans.toString(), 2);
                if (ik <= (max / 2)) {
                    flag2 = false;
                    for (int[] ints : matrix) {
                        ints[i] = ints[i] == 0 ? 1 : 0;
                    }
                }
            }
        }
        int sum = 0;
        for (int[] ints : matrix) {
            StringBuilder ans = new StringBuilder();
            for (int m : ints) {
                ans.append(m);
            }
            sum += Integer.parseInt(ans.toString(), 2);
        }
        return sum;
    }
}
